package com.emm.yixun.website.controller.customer;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emm.yixun.common.model.BusinessAbnorma;
import com.emm.yixun.common.model.NeedReceiveRefund;
import com.emm.yixun.common.model.dto.BusinessAbnormaDto;
import com.emm.yixun.common.model.dto.HisAuditRecordDto;
import com.emm.yixun.common.model.dto.NeedReceiveRefundDto;
import com.emm.yixun.common.model.dto.SubscribeDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.BusinessAbnormaEnum;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.model.enums.DocTypeEnum;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.marketing.service.BusinessAbnormaServiceFacade;
import com.emm.yixun.marketing.service.NeedReceiveRefundServiceFacade;
import com.emm.yixun.website.controller.base.BaseController;




@Controller
@RequestMapping("/activitiInfo/")
public class ActivitiInfoController extends BaseController{
	
	@Autowired
	private BusinessAbnormaServiceFacade businessAbnormaServiceFacade;
	@Autowired
	private NeedReceiveRefundServiceFacade needReceiveRefundServiceFacade;
	
	// 分页查询
	@RequestMapping(value = "hisAuditRecordList")
	@ResponseBody
	public List<HisAuditRecordDto> hisAuditRecordList(HisAuditRecordDto dto, Integer limit, Integer page) {
		List<HisAuditRecordDto> harDtoList = new ArrayList<HisAuditRecordDto>();
		try {
			String businessType = dto.getBusinessType();
			String procinstName = BusinessAbnormaEnum.getByNo(businessType).getName();
			if(BusinessAbnormaEnum.BusinessAbnorma_12.getNo().equals(businessType)) {
				//延期收款绑定的是代收款业务ID,需通过签约ID查出所有代收款记录,再去匹配是否存在已发起或已处理流程
				String businessId = dto.getBusinessId();
				String[] businessIds = businessId.split("[,]");
				if(businessIds.length > 0) {
					for(String str: businessIds) {
						Response<NeedReceiveRefund> nrrRes = needReceiveRefundServiceFacade.findById(Long.valueOf(str));
						if(nrrRes.isSuccess() && null != nrrRes.getEntity()) {
							NeedReceiveRefund nrr = nrrRes.getEntity();
							List<HisAuditRecordDto> returnList = getHisAuditRecordDto(String.valueOf(nrr.getId()), businessType, procinstName);
							if(null != returnList && returnList.size() > 0) {
								harDtoList.addAll(returnList);
							}
						}
					}
				}
			}else {
				List<HisAuditRecordDto> returnList = getHisAuditRecordDto(dto.getBusinessId(), businessType, procinstName);
				if(null != returnList && returnList.size() > 0) {
					harDtoList.addAll(returnList);
				} 
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return harDtoList;
	}
	
	public List<HisAuditRecordDto> getHisAuditRecordDto(String businessId, String businessType, String procinstName) {
		List<HisAuditRecordDto> harDtoList = new ArrayList<HisAuditRecordDto>();
		BusinessAbnormaDto businessAbnormaDto = new BusinessAbnormaDto();
		businessAbnormaDto.setBusinessId(Long.valueOf(businessId));
		businessAbnormaDto.setBusinessType(businessType);
		businessAbnormaDto.setOrders("create_time desc");
		ResponseList<BusinessAbnorma> businessAbnormas = businessAbnormaServiceFacade.findByDto(businessAbnormaDto);
		if(businessAbnormas.isSuccess() && null != businessAbnormas.getEntity()
				&& businessAbnormas.getEntity().size() != 0) {
			List<BusinessAbnorma> baList = businessAbnormas.getEntity();
			for(BusinessAbnorma ba: baList) {
				//过滤掉待提交及审核中的记录
				if(AuditStatusEnum.TOSUBMIT.getNo().equals(ba.getAuditStatus())
						|| AuditStatusEnum.AUDIT.getNo().equals(ba.getAuditStatus())) {
					continue;
				}
				HisAuditRecordDto harDto = new HisAuditRecordDto();
				String startTime = DateUtils.getDateToString(ba.getCreateTime(), DateUtils.parsePatterns[3]);
				String endTime = DateUtils.getDateToString(ba.getCheckDate(), DateUtils.parsePatterns[3]);
				String originDelayDate = DateUtils.getDateToString(ba.getConversionSignDate(), DateUtils.parsePatterns[1]);
				String delayDate = DateUtils.getDateToString(ba.getDelaydate(), DateUtils.parsePatterns[1]);
				String delayDays = String.valueOf(DateUtils.getDaysSub(ba.getConversionSignDate(), ba.getDelaydate()));
				String auditStatus = AuditStatusEnum.getByNo(ba.getAuditStatus()).getDes();
				harDto.setProcinstName(procinstName);
				harDto.setStartTime(startTime);
				harDto.setEndTime(endTime);
				harDto.setOriginDelayDate(originDelayDate);
				harDto.setDelayDate(delayDate);
				harDto.setDelayDays(delayDays);
				harDto.setAuditStatus(auditStatus);
				harDtoList.add(harDto);
			}
		}
		return harDtoList;
	}
}
